package com.server.common.api;

import com.server.common.enums.ResultCodeEnum;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;

/**
 * @Author zgr
 * @create 2023/7/9 17:31
 */
@Getter
@Setter
@ToString
@NoArgsConstructor
public class ApiResult<T> implements Serializable {

    private static final long serialVersionUID = -2904860769311841748L;

    private Integer code;
    private String message;
    private T data;
    private Boolean success;

    public ApiResult(Integer code, String message, T data, Boolean success) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.success = success;
    }

    public static <T> ApiResult<T> success(T data) {
        return new ApiResult<>(ResultCodeEnum.Success.getCode(),
                ResultCodeEnum.Success.getMessage(),
                data,
                true);
    }

    public static <T> ApiResult<T> fail(int code, String message) {
        return new ApiResult<>(code, message, null, false);
    }

    public static <T> ApiResult<T> fail(String message) {
        return new ApiResult<>(ResultCodeEnum.Fail.getCode(), message, null, false);
    }

    public static <T> ApiResult<T> status(boolean flag) {
        return flag ? success(null) : fail(ResultCodeEnum.Fail.getCode(), ResultCodeEnum.Fail.getMessage());
    }

}
